package com.example.demo.comtroller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 郝文雪
 * @date 2022/7/15 21:43
 * @Description Test
 */
@Controller
public class RespController {
    /**
     * 请求重定向的方法1
     */
    @RequestMapping("/rt")
    public Object redirect(){
        return "redirect:hello.html";
    }


    /**
     * 请求重定向的方法2
     */
    @RequestMapping("/rt2")
    public void redirect(HttpServletResponse response) throws IOException {
        response.sendRedirect("hello.html");
    }


    /**
     * 请求转发的实现方法1
     * @return
     */
    @RequestMapping("/fw")
    public Object forward(){
        return "forward:hello.html";
    }

    /**
     * 请求转发实现方式2
     */
    @RequestMapping("/fw2")
    public void forward2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("hello.html").forward(request,response);
    }

    @RequestMapping("/index")
    @ResponseBody
    public String getIndex(){
        return "index.html";
    }
}
